# 题目四：新建“test4”Python文件：读取素材中的“test.txt”文件，
# 使用jieba库进行分词，并将出现次数前5的词汇和出现次数信息写入到“output.txt”文件中。

import jieba

fo = open('test.txt', 'r', encoding='utf-8').read()
fw = open('output.txt', 'w')

words = jieba.lcut(fo)
word_num = {}

for i in words:
    if len(i) > 1:
        word_num[i] = word_num.get(i, 0) + 1

lists = list(word_num.items())
lists.sort(key=lambda x: x[1],reverse=True)

line = 5

fw.write(str(lists[:line]))

fw.close()
